package com.quanyan.authority.service.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.authority.mapper.BusinessMapper;
import com.quanyan.authority.mapper.TbBusinessMapper;
import com.quanyan.authority.model.db.TbBusiness;
import com.quanyan.authority.model.request.ReqBusiness;
import com.quanyan.authority.model.request.ReqPage;
import com.quanyan.authority.service.BusinessService;
import com.quanyan.common.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/1.
 */
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public TbBusiness findById(Integer id) {
        if(id!=null)
            return tbBusinessMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public TbBusiness add(ReqBusiness reqBaseParam) {
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setName(reqBaseParam.getName());
        tbBusiness.setCode(reqBaseParam.getCode());
        tbBusiness.setRemark(reqBaseParam.getRemark());
        tbBusiness.setCreateTime(new Date());
        tbBusiness.setUpdateTime(new Date());
        tbBusiness.setIsDelete(Constants.IS_DELETE_FALSE);
        tbBusinessMapper.insertSelective(tbBusiness);
        return tbBusiness;
    }

    @Override
    public void del(Integer id) {
        tbBusinessMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(ReqBusiness reqBaseParam) {
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setName(reqBaseParam.getName());
        tbBusiness.setCode(reqBaseParam.getCode());
        tbBusiness.setRemark(reqBaseParam.getRemark());
        tbBusiness.setSysId(reqBaseParam.getSystemId());
        tbBusiness.setIsDelete(reqBaseParam.getIsDelete().byteValue());
        tbBusinessMapper.updateByPrimaryKeySelective(tbBusiness);
    }

    @Override
    public Page findAll(ReqPage reqPage, ReqBusiness reqBaseParam) {
        Page page = new Page();
        page.setCurrentPage(reqPage.getPageNum());
        page.setShowCount(reqPage.getPageSize());
        List<TbBusiness> lst = businessMapper.findAll(page,reqBaseParam.toMap());
        page.setObject(lst);
        return page;
    }
}
